package game.dinowar;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.os.SystemClock;

public class OneEnemy {

	public AnimatedSprite OneEnemy_AnimatedSprite;

	private Scene mScene;

	public int speed = 1;

	private int loai = 0;

	public OneEnemy(Enemy enemy, Scene mScene, float pX, float pY,
			TiledTextureRegion Enemy_TiledTextureRegion) {
		this.mScene = mScene;
		loai = Tools.getRandomIndex(0, 6);
		OneEnemy_AnimatedSprite = new AnimatedSprite(pX, pY,
				Enemy_TiledTextureRegion);
		statusMoveLeft();
		mScene.attachChild(OneEnemy_AnimatedSprite);
	}

	public void statusMoveUp() {
		if (loai == 0)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 3, 5,
					true);
		else if (loai == 1)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 6, 8,
					true);
		else if (loai == 2)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 9,
					11, true);
		else if (loai == 3)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 48,
					50, true);
		else if (loai == 4)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 51,
					53, true);
		else if (loai == 5)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 54,
					56, true);
		else if (loai == 6)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 57,
					59, true);
	}

	public void statusMoveRight() {
		if (loai == 0)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 15,
					17, true);
		else if (loai == 1)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 18,
					20, true);
		else if (loai == 2)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 21,
					23, true);
		else if (loai == 3)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 60,
					62, true);
		else if (loai == 4)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 63,
					65, true);
		else if (loai == 5)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 66,
					68, true);
		else if (loai == 6)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 69,
					71, true);
	}

	public void statusMoveDown() {
		if (loai == 0)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 27,
					29, true);
		else if (loai == 1)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 30,
					32, true);
		else if (loai == 2)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 33,
					35, true);
		else if (loai == 3)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 72,
					74, true);
		else if (loai == 4)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 75,
					77, true);
		else if (loai == 5)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 78,
					80, true);
		else if (loai == 6)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 81,
					83, true);
	}

	public void statusMoveLeft() {
		if (loai == 0)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 39,
					41, true);
		else if (loai == 1)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 42,
					44, true);
		else if (loai == 2)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 45,
					47, true);
		else if (loai == 3)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 84,
					86, true);
		else if (loai == 4)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 87,
					89, true);
		else if (loai == 5)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 90,
					92, true);
		else if (loai == 6)
			OneEnemy_AnimatedSprite.animate(new long[] { 100, 100, 100 }, 93,
					95, true);
	}

	public void moveXY(float pX, float pY) {
	//	if (!enemy.collidesWith(pX, pY))
			OneEnemy_AnimatedSprite.setPosition(pX, pY);
	}

	// =============================================================================
	public void moveRelativeXY(float pX, float pY) {
		OneEnemy_AnimatedSprite.setPosition(
				OneEnemy_AnimatedSprite.getX() + pX,
				OneEnemy_AnimatedSprite.getY() + pY);
	}

	long time = SystemClock.elapsedRealtime();
	int huong = 0;

	public void moveRandom() {
		// if (SystemClock.elapsedRealtime() - time > 3000) {
		huong = Tools.getRandomIndex(0, 3);
		time = SystemClock.elapsedRealtime();
		if (huong == 0)
			statusMoveLeft();
		else if (huong == 1)
			statusMoveRight();
		else if (huong == 2)
			statusMoveUp();
		else if (huong == 3)
			statusMoveDown();
		// }

		if (huong == 0) {
			moveXY(OneEnemy_AnimatedSprite.getX(),
					OneEnemy_AnimatedSprite.getY() - 40);
			// if (!enemy.collidesWith(
			// OneEnemy_AnimatedSprite.getX() - speed,
			// OneEnemy_AnimatedSprite.getY()
			// + (OneEnemy_AnimatedSprite.getHeight() / 2)))
			// moveRelativeXY(-speed, 0);
		} else if (huong == 1) {
			moveXY(OneEnemy_AnimatedSprite.getX() + 40,
					OneEnemy_AnimatedSprite.getY());
			// if (!enemy.collidesWith(
			// OneEnemy_AnimatedSprite.getX()
			// + OneEnemy_AnimatedSprite.getWidth() + speed,
			// OneEnemy_AnimatedSprite.getY()
			// + (OneEnemy_AnimatedSprite.getHeight() / 2)))
			// moveRelativeXY(speed, 0);
		} else if (huong == 2) {
			moveXY(OneEnemy_AnimatedSprite.getX(),
					OneEnemy_AnimatedSprite.getY() + 40);
			// if (!enemy.collidesWith(OneEnemy_AnimatedSprite.getX()
			// + (OneEnemy_AnimatedSprite.getWidth() / 2),
			// OneEnemy_AnimatedSprite.getY() - speed))
			// moveRelativeXY(0, -speed);
		} else if (huong == 3) {
			moveXY(OneEnemy_AnimatedSprite.getX() - 40,
					OneEnemy_AnimatedSprite.getY());
			// if (!enemy.collidesWith(
			// OneEnemy_AnimatedSprite.getX()
			// + +(OneEnemy_AnimatedSprite.getWidth() / 2),
			// OneEnemy_AnimatedSprite.getY()
			// + OneEnemy_AnimatedSprite.getWidth() + speed))
			// moveRelativeXY(0, speed);
		}
	}

	public void deleteQuaivat_1() {
		this.mScene.detachChild(OneEnemy_AnimatedSprite);
	}

	long time_reset_begin = 0;
	public boolean bool_reset = false;

	public void reset() {
		if (bool_reset) {
			if (!this.OneEnemy_AnimatedSprite.isVisible()
					&& time_reset_begin == 0)
				time_reset_begin = SystemClock.elapsedRealtime();

			if (time_reset_begin != 0
					&& SystemClock.elapsedRealtime() - time_reset_begin > 5000) {

				this.OneEnemy_AnimatedSprite.setPosition(416, 128);
				this.OneEnemy_AnimatedSprite.setVisible(true);
				time_reset_begin = 0;
				bool_reset = false;
			}
		}
	}
}
